<?php
class Delivery_Model_Mapper_Zonas
	extends Delivery_Model_Mapper_Abstract
{
	
	public function getDbTable()
	{
		return parent::getDbTable( 'Zonas' );
	}
	
	public function save( $zona )
	{
		
		$dbTable= $this->getDbTAble();
		
		if ( $zona->getId() == null ){		
			$zona->idZona = $dbTable->insert( $zona );
		} else {
			$zona->idZona = $dbTable->update( $zona );
		}
		
	}
	
	public function delete( $zona )
	{
		$dbTable= $this->getDbTAble();
		
		$this->logDeleted( $zona );
		
		$dbTable->delete( $zona );
	}
	
	/**
	 * @param int $idZona
	 * @return object Delivery_Model_Zona
	 */
	public function getById( $idZona )
	{
		if ( $this->getIdentityMap()->isRegistered( 'Delivery_Model_Zona', $idZona )){
			return $this->getIdentityMap()->get( 'Delivery_Model_Zona', $idZona );
		}
		
		$dbTable= $this->getDbTAble();
		$row	= $dbTable->getById( $idZona );
	
		if( empty($row) ){
			throw new Delivery_Model_Exception( Delivery_Model_Exception::ZONE_NOT_FOUND );
		}
		
		return $this->_fetch( $row );
	}
	
	public function getByDireccion( $idNegocio, $idDireccion ){
		
		$dbTable= $this->getDbTAble();
		$row	= $dbTable->getByDireccion( $idNegocio, $idDireccion );
	
		if( empty($row) ){
			throw new Delivery_Model_Exception( Delivery_Model_Exception::ZONE_NOT_FOUND );
		}
		
		return $this->_fetch( $row );
	}
	
	public function getByIds( $ids )
	{
		$dbTable= $this->getDbTAble();
		
		return $this->_fetchCollection(
			'Zonas',
			$dbTable->getByIds( $ids )
		);
	}
	
	public function getByNegocio( $idNegocio )
	{
		$dbTable= $this->getDbTAble();
		
		return $this->_fetchCollection(
			'Zonas',
			$dbTable->getByNegocio( $idNegocio )
		);
	}
	
	public function getNegociosByDireccion( $idDireccion )
	{
		$dbTable= $this->getDbTAble();
		return $dbTable->getNegociosByDireccion( $idDireccion );
	}
	
		
	
	/**
	 * @param array
	 * @return object Delivery_Model_Zona
	 */
	protected function _fetch( $row )
	{
		$zona = new Delivery_Model_Zona( $row );
		
		$this->getIdentityMap()->set( $zona, $zona->getId() );
		
		return $zona;
	}
	
}